package com.yuke.cloud.service.pmc.controller.frontend;

import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.pmc.dto.ProdIdsDto;
import com.yuke.cloud.service.pmc.dto.RegionResortIntroducePictureDto;
import com.yuke.cloud.service.pmc.entity.ProdRegionResortIntroduce;
import com.yuke.cloud.service.pmc.service.ProdRegionResortIntroduceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.service.pmc.service.ProdRegionResortPictureService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import com.yuke.cloud.common.core.support.BaseController;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by zys on 2018/09/30.
*/
@Slf4j
@Api(value = "WEB - ProdRegionResortIntroduceController", tags = "处理ProdRegionResortIntroduce相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/prod/region_resort_introduce")
public class ProdRegionResortIntroduceController extends BaseController {
    @Resource
    private ProdRegionResortIntroduceService prodRegionResortIntroduceService;
    @Resource
    private ProdRegionResortPictureService prodRegionResortPictureService;

    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "prodRegionResortIntroduce", value = "该数据记录的POJO") @RequestBody ProdRegionResortIntroduce prodRegionResortIntroduce) {
        Example example = new Example(ProdRegionResortIntroduce.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("regionId", prodRegionResortIntroduce.getRegionId());
//        if (prodRegionResortIntroduceService.selectCountByExample(example) > 0) {
//            return  WrapMapper.error("该风景介绍已存在");
//        }
        prodRegionResortIntroduceService.save(prodRegionResortIntroduce);
        if (prodRegionResortIntroduce.getProdRegionResortPictures() != null && prodRegionResortIntroduce.getProdRegionResortPictures().size() > 0) {
            prodRegionResortIntroduce.getProdRegionResortPictures()
                    .forEach(prodRegionResortPicture -> {
                        prodRegionResortPicture.setResortId(prodRegionResortIntroduce.getResortIntroduceId());
                        prodRegionResortPictureService.save(prodRegionResortPicture);
                    });
        }
        return WrapMapper.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        prodRegionResortIntroduceService.deleteById(id);
        return WrapMapper.ok();
    }

    @PostMapping("/ids")
    @ApiOperation(httpMethod = "POST", value = "根据ids删除多条记录")
    public Wrapper deleteByIds(@ApiParam(name = "factoryIds", value = "记录id") @RequestBody ProdIdsDto prodIdsDto) {
        prodRegionResortIntroduceService.deleteByIds(prodIdsDto.getIds());
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "prodRegionResortIntroduce", value = "需要修改的内容") @RequestBody ProdRegionResortIntroduce prodRegionResortIntroduce) {
        prodRegionResortIntroduceService.update(prodRegionResortIntroduce);
        return WrapMapper.ok();
    }

    @PostMapping("/condition")
    @ApiOperation(httpMethod = "POST", value = "根据ID获取具体信息")
    public Wrapper<List<ProdRegionResortIntroduce>> detail(@RequestBody(required = false) ProdRegionResortIntroduce prodRegionResortIntroduce) {
        List<ProdRegionResortIntroduce> prodRegionResortIntroduces = prodRegionResortIntroduceService.getByCondition(prodRegionResortIntroduce);
        return WrapMapper.ok(prodRegionResortIntroduces);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<ProdRegionResortIntroduce>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                    @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<ProdRegionResortIntroduce> list = prodRegionResortIntroduceService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    /*@GetMapping("/app/recommend")
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<List<RegionResortIntroducePictureDto>> listByRecommend() {
        List<RegionResortIntroducePictureDto> list = prodRegionResortIntroduceService.findByRecommend();
        return WrapMapper.ok(list);
    }*/
}
